package com.zut.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.zut.pojo.Category;
import com.zut.pojo.FoodCategory;
import com.zut.service.FoodCategoryService;
import com.zut.service.impl.FoodCategoryServiceImpl;
import com.zut.vo.FoodCategoryVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 张晨
 * @date 2024/9/13 9:49
 * @desc
 */

@WebServlet("/foodCategory")
public class FoodCategoryController extends HttpServlet {

    private FoodCategoryService foodCategoryService = new FoodCategoryServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String op = req.getParameter("op");

        if (StringUtils.isEmpty(op)){
            resp.getWriter().write("参数错误");
            return;
        }

        switch (op){
            case "sel":
                selFoodCategory(req,resp);
                break;
            case "add":
                addFoodCategory(req,resp);
                break;
            case "del":
                delFoodCategory(req,resp);
                break;
            case "upd":
                updFoodCategory(req,resp);
                break;
        }
    }

    private void updFoodCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer categoryId = Integer.parseInt(req.getParameter("categoryId"));
        String categoryName = req.getParameter("categoryName");
        FoodCategory foodCategory = new FoodCategory();

        foodCategory.setCategoryName(categoryName);
        foodCategory.setCategoryId(categoryId);

        Integer result = foodCategoryService.updateById(foodCategory);

        resp.getWriter().write(result+"");
    }

    private void delFoodCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer categoryId = Integer.parseInt(req.getParameter("categoryId"));

        Integer result = foodCategoryService.delById(categoryId);

        resp.getWriter().write(result+"");
    }

    private void addFoodCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer shopId = Integer.parseInt(req.getParameter("shopId"));
        String categoryName = req.getParameter("categoryName");
        FoodCategory foodCategory = new FoodCategory();

        foodCategory.setCategoryName(categoryName);
        foodCategory.setShopId(shopId);

        Integer result = foodCategoryService.add(foodCategory);

        resp.getWriter().write(result+"");
    }

    private void selFoodCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String shopId = req.getParameter("shopId");

        if (StringUtils.isEmpty(shopId)){
            resp.getWriter().write("参数错误");
            return;
        }

        List<FoodCategoryVo> lists = foodCategoryService.selectByShopId(Integer.parseInt(shopId));


        resp.getWriter().write(JSON.toJSONString(lists));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
